<?php
namespace App\Models;

class Project extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "project";
    }

    private function t() {
        return 'App\Models\Project';
    }

    /**
     * 统计会员营销项目总数
     *
     * @return int|false
     */
    public function total() {
        $query = $this->select('COUNT(*) AS cnt')->from($this->t());
        try {
            $ret = $query->getQuery()->execute();
            if($ret != false) {
                $ret = $ret->toArray();
                if(is_array($ret) && count($ret) > 0 && isset($ret[0]['cnt'])) {
                    return intval($ret[0]['cnt']);
                }
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取会员营销项目信息
     *
     * @param int $offset 起始位置
     * @param int $limit 限制条数
     *
     * @return array|false
     */
    public function gets($offset, $limit) {
        $query = $this->select('*')->from($this->t());
        try {
            $ret = $query->orderBy('create_at DESC')->limit($limit, $offset)->getQuery()->execute();
            if($ret !== false) {
                return $ret->toArray();
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 添加会员营销项目信息
     *
     * @param array $entity 信息实体
     *
     * @return int|false
     */
    public function add($entity) {
        try {
            if($this->db()->insertAsDict($this->getSource(), $entity)) {
                $this->clearIdAndNameCache();
                return $this->db()->lastInsertId();
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 编辑会员营销项目信息
     *
     * @param array $entity 信息实体
     *
     * @return int|false
     */
    public function edit($entity) {
        try {
            $ret = $this->db()->updateAsDict($this->getSource(), $entity, sprintf("id=%d LIMIT 1", intval($entity['id'])));
            $this->clearIdAndNameCache();
            return $ret;
        } catch(\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    /**
     * 获取某个具体的会员营销项目信息
     *
     * @param int $id id值
     *
     * @return array|false
     */
    public function get($id) {
        if($id == '' || !is_numeric($id)) {
            return false;
        }
        //@formatter:off
        $ret = $this->select('*')
            ->from($this->t())
            ->where('id =:id:', array('id' => $id))
            ->limit(1)
            ->getQuery()
            ->execute();
        //@formatter:on
        if($ret !== false) {
            $ret = $ret->toArray();
            if(count($ret) > 0) {
                return $ret[0];
            }
        }
        return false;
    }

    /**
     * 获取所有的会员营销项目的id值和名称
     *
     * @return array|false
     */
    public function getIdAndName() {
        $mkey = md5(__CLASS__ . '::getIdAndName()');
        $ret  = $this->memCache()->get($mkey);
        if($ret == '') {
            $query = $this->select(['id', 'name', 'info_type'])->from($this->t())->orderBy('id DESC');
            try {
                $ret = $query->getQuery()->execute();
                if($ret !== false) {
                    $ret = $ret->toArray();
                    $this->memCache()->save($mkey, $ret, 30 * 24 * 60 * 60);
                    return $ret;
                }
            } catch(Exception $ex) {
                $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
            }
            return false;
        }
        return $ret;
    }

    /**
     * 清空IdAndName的Cache
     */
    private function clearIdAndNameCache() {
        $mkey = md5(__CLASS__ . '::getIdAndName()');
        $this->memCache()->save($mkey, '');
        $this->getIdAndName();
    }

    /**
     * 删除某个具体的会员营销项目信息
     *
     * @param int $id id值
     *
     * @return int
     */
    public function remove($id) {
        return $this->db()->delete($this->getSource(), sprintf("id=%d LIMIT 1", intval($id)));
    }
}

